/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.Utilities
{
    class FeedSyncUpdate
    {
        static string s_SourcePath;
        static string s_ID;
        static string s_Title;
        static string s_Description;
        static string s_By = "FeedSyncUpdate.exe";

        static bool s_ResolveConflicts;

        static void Main(string[] i_Arguments)
        {
            try
            {
                //  Validate arguments
                if (i_Arguments.Length < 4)
                {
                    Microsoft.CSA.FeedSync.Utilities.FeedSyncUpdate.DisplayUsage(null);
                    return;
                }

                //  Get required parameters
                s_SourcePath = i_Arguments[0];
                s_ID = i_Arguments[1];
                s_Title = i_Arguments[2];
                s_Description = i_Arguments[3];

                //  Get optional parameters
                if (i_Arguments.Length > 4)
                    s_By = i_Arguments[4];

                if (i_Arguments.Length > 5)
                    s_ResolveConflicts = System.Convert.ToBoolean(i_Arguments[5]);

                //  Create new instance of xml document
                System.Xml.XmlDocument SourceXmlDocument = new System.Xml.XmlDocument();

                try
                {
                    //  Load the feed contents into xml document
                    SourceXmlDocument.Load(s_SourcePath);
                }
                catch (System.Exception Exception)
                {
                    System.Console.WriteLine("Exception while loading '" + s_SourcePath + "': " + Exception.Message);
                    return;
                }

                //  Create an instance of Feed
                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed RSSFeed = new Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed(SourceXmlDocument);

                //  Get existing node
                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeedSyncItem RSSFeedSyncItem;
                if (!RSSFeed.FindItem(s_ID, out RSSFeedSyncItem))
                {
                    System.Console.WriteLine("Specified item does not exist!");
                    return;
                }

                //  Update existing node
                RSSFeedSyncItem.Title = s_Title;
                RSSFeedSyncItem.Description = s_Description;

                if (s_ResolveConflicts)
                {
                    RSSFeedSyncItem.ResolveConflicts
                        (
                        System.DateTime.Now,
                        s_By
                        );
                }
                else
                {
                    RSSFeedSyncItem.Update
                        (
                        System.DateTime.Now,
                        s_By
                        );
                }

                //  Output updated feed
                System.Console.WriteLine(RSSFeed.XmlDocument.OuterXml);
            }
            catch (System.Exception Exception)
            {
                System.Console.WriteLine("Exception encountered: " + Exception.Message);
            }
        }

        static void DisplayUsage(string i_Text)
        {
            string Text = "Usage:\r\nFeedSyncUpdate [SourcePath] [ID] [Title] [Description] [By]\r\n\r\nParameters:\r\n  SourcePath=fully qualified filename of original FeedSync document\r\n  GUID=unique value for new item\r\n  Title=value of the title attribute of the new item element (use quotes to enter multiple words)\r\n  Description=value of the title attribute of the new item element (use quotes to enter multiple words)\r\n  By=unique entity making the change (optional - default is \"\")\r\n";

            //  If text was provided, prepend it to default usage text
            if (!System.String.IsNullOrEmpty(i_Text))
                Text = i_Text + "\r\n\r\n" + Text;

            System.Console.WriteLine(Text);
        }
    }
}
